package com.kedsoft.andaily.controller;


import com.kedsoft.andaily.common.CommonResult;
import com.kedsoft.andaily.common.MessageResult;
import com.kedsoft.andaily.entity.Depart;
import com.kedsoft.andaily.entity.User;
import com.kedsoft.andaily.service.DepartService;
import com.kedsoft.andaily.vo.DepartVo;
import com.kedsoft.andaily.vo.PageVo;
import com.kedsoft.andaily.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-04-02
 */
@Controller
@RequestMapping("/depart")
public class DepartController {
    @Autowired
    private DepartService departService;

    @RequestMapping(value = "index")
    public String index() {
        return "depart/index";
    }

    @RequestMapping(value = "all")
    @ResponseBody
    public List<Map<String,Object>> all(HttpServletRequest request){
        User session = (User)request.getSession().getAttribute("loginUser");
        Map<String, Object> query = new HashMap<>();
        query.put("departid",session.getDepartid());
        List<Map<String,Object>> tree = departService.getDepartTree(query);
        return tree;
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Depart> getPageList(HttpServletRequest request, Pagination<Depart> pagination, @RequestParam(required = false) String departname){
        Map<String, Object> query = new HashMap<>();
        query.put("departname", departname);
        User session = (User)request.getSession().getAttribute("loginUser");
        query.put("departid",session.getDepartid());
        departService.pageList(pagination,query);
        PageVo<Depart> pageVo = new PageVo<>(pagination.getItems(),pagination.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "findDepart", method = RequestMethod.GET)
    @ResponseBody
    public Depart findDepart(Integer id){
        Depart depart = departService.selectById(id);
        return depart;
    }

    @RequestMapping("form")
    public String form(){
        return "depart/form";
    }

    @RequestMapping("edit")
    public String edit(){
        return "depart/edit";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(DepartVo param){
        Depart bean = new Depart();
        bean.setDepartname(param.getDepartname());
        bean.setPid(param.getPid());
        bean.setPname(param.getPname());
        bean.setCode(param.getCode());
        bean.setStatus(param.getStatus());
        bean.setCrtime(new Date());
        departService.insert(bean);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(DepartVo param){
        Depart bean = new Depart();
        bean.setDepartname(param.getDepartname());
        bean.setPid(param.getPid());
        bean.setPname(param.getPname());
        bean.setCode(param.getCode());
        bean.setStatus(param.getStatus());
        bean.setId(param.getId());
        bean.setUptime(new Date());
        departService.updateById(bean);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        departService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            departService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "depart/search";
    }

    @RequestMapping(value = "nextcode", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<String> nextCode(@RequestParam Integer pid){
        String code = createCode(pid);
        CommonResult<String> result = new CommonResult<>();
        result.setCode("200");
        result.setMessage("success");
        result.setResult(code);
        return result;
    }

    //获取最大的code
    private String getMaxCode(int pid){
        return departService.maxCode(pid);
    }

    //获取最大的下一个code
    private String createCode(int pid){
        String maxCode = getMaxCode(pid);
        String pCode = getParentDepartById(pid);
        if(pCode==null){
            pCode = "3201";
        }
        String nextcode = getNextCode(maxCode,pCode);
        return nextcode;
    }

    private String getNextCode(String newcode, String pcode){
        String nextcode=null;
        if(newcode == null || newcode.equals("")){
            if(pcode != null && !pcode.equals("")){
                nextcode = String.valueOf(Long.parseLong(pcode) * 100 + 1);
            }
        }else{
            nextcode = String.valueOf(Long.parseLong(newcode) + 1);
        }
        return nextcode;
    }

    private String getParentDepartById(int pid){
        if(pid==0){
            return null;
        }
        Depart depart = departService.selectById(pid);
        return depart.getCode();
    }
}

